package main

import (
	"context"
	"os"
	"os/signal"
	"syscall"

	ytask "github.com/gojuukaze/YTask/v2"
)

// 定义两个任务，任务参数、返回值支持所有能被序列化为json的类型

func demo(a int, b int) int {
	return a + b
}

type User struct {
	Id   int
	Name string
}

func appendUser(user User, ids []int, names []string) []User {
	var r = make([]User, 0)
	r = append(r, user)
	for i := range ids {
		r = append(r, User{ids[i], names[i]})
	}
	return r
}

func main() {
	// RedisBroker最后一个参数是连接池大小， Server端无需设置
	broker := ytask.Broker.NewRedisBroker("127.0.0.1", "6379", "", 14, 0)

	// RedisBackend最后一个参数是连接池大小，对于server端 如果<=0 会使用默认值，
	// 默认值是 min(10, numWorkers)
	backend := ytask.Backend.NewRedisBackend("127.0.0.1", "6379", "", 15, 0)

	ser := ytask.Server.NewServer(
		ytask.Config.Broker(&broker),
		ytask.Config.Backend(&backend),
		ytask.Config.Debug(true),
		ytask.Config.StatusExpires(60*5),
		ytask.Config.ResultExpires(60*5),
	)

	// 注册任务
	ser.Add("group1", "add", demo)
	ser.Add("group1", "append_user", appendUser)

	// 运行server，并发数3
	ser.Run("group1", 3)

	quit := make(chan os.Signal, 1)

	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit
	ser.Shutdown(context.Background())

}
